package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.List;

import static java.lang.Thread.sleep;


public class Main {
    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");

        // 创建Chrome浏览器实例
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);

        // 打开百度页面
        webDriver.get("https://www.baidu.com");


        //找到输入搜索框
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //向搜索框中输入软件测试
        search_input.sendKeys("软件测试");
        sleep(2000);
        //找到百度一下
        WebElement baidu_button = webDriver.findElement(By.cssSelector("#su"));
        //点击百度一下按钮
        sleep(2000);
        baidu_button.click();
        //找到页面上所有与"软件测试"相关的元素
        List<WebElement> search_result = webDriver.findElements(By.cssSelector("a em"));
        sleep(2000);
        //遍历list
        //如果搜索框中有软件测试,测试通过,否则不通过
        for (int i = 0; i < search_result.size(); i++) {
            if (search_result.get(i).getText().equals("软件测试")) {
                System.out.println("测试通过");
            } else {
                System.out.println("测试不通过");
            }
        }
        webDriver.quit();
    }
}